package com.shop.shopmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.shop.shopmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116699105";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCadpx3k9pA96yb0iMwVIjWhNKpfgPP+tTfBEnglgiu8YibniU2lr77eDFUQuzZ2yP5bO8u/XGNf4qoUusew/VywuTFUAFdyH69/cjUo/wZem8xHMzHYwR1WLV2SuXmZvvrNO/5jrVDzsA904tMyV0pR5W6vNPRfwDbqb/o1e9GVpIKNY5iJ/EMTFpTnKkxB1D2VarsWhzMWKcGBxA8IOKCOJlNsPNLYHCoU/NXjHdfYG5xY/KGLcpMHaEfDLYU7f9amMNUv40MkWPZmJTJlLIfvWQLFE2cUUZ31pYxl6MMjpXDbfurUFc8aobL5QZiYcU3Hyqs2UPUQi/FMHT6qqaTAgMBAAECggEAZ0PA21inYyOx7AKSr+70BKaLx/CNT/AbfHV7hoAHkh27/H2/1zz0tV3nbLPJcORGqqwfQ8UAs15/LoUw10Kex3hwRVF1eGNgCFiweSigk8/8olLkvK///QuvVURBasWSye58ONUlbg29DBhvWoiMldpbyZoP6lEEG9bijCDqdJTE6SIgYLWXTgcIgnW58bQ0hsgum8Gf+vGajgu+5cJiVOrhKtxxP/h3W40ea+W/7Ymb9vP7PFjo4L2OJZW0vfKDXpvw6fes2b0qn9CnnY6t8716xygx1RYhSJdC2SD09Dy6p86dAYMtHebuMlmX7lgvp+IPlfngDsvVU9sc33wGIQKBgQDZEuYEb2U7775IMMJOwGviA1qV2hYt+eDWSDgOGRuM26ZoZBV5pccOKnG9ml90eoqh1kthK4sGwEUpD+TGd06aZ75O4IHAP6NFEoPha4w/rP4k+4hCQwoSvZpHfmVsU3jV6Racn8AxLiVi/2u6OafDQIV1A519GiX5lKr824NG6wKBgQC2KXu5v/Z2sDzXCFMrhl18Xs0aNqRLlJUpSVi4NDEA9NLTE86Qj2vWahXNfvuyyspQptQrf7+5x23C12dyOsD1t41X5QRrCnSiBsLDbxqrBDz1b8wHoAwd3vL1jAnNzR2/u5+qlMm1g/sBOYnL0JGKNQoDTRIvuziSgkW7Ft0E+QKBgDH0gW6/3AJBF/8xCWZqLemjb/NQbw5BlRZ7JDtKV+bAnbkkXzTRi8+cwPQrVJBPqxu3Xz2LccEp+kCK4dEyn6o0+bzZOcLuYjqBGkyEHZCB1TLZl+/30ZeifqC5W8nkI8jGjWeZ8/bdcckokrgWkJ8EeDIHlPS3UcnwBrGOKtXlAoGBAJ+qHqNCKaIAeWM1DckxnZFcesPGLx18H32vp07VmDoxXPTcAIte2nN2SqPy8GTXMJuejouODpukvIUyTp6l9umot//ZYZT89hvqdx3brXFGZrPuCNSKKPSprSUiWJmFdtTOfHWEsoQiKYj6SNhXeKm+MYeRLyadIEnyW3JP6OvpAoGAFra//H4b4lDb0Zsyowl94SdK3ySwXoOZsMN74I193500m9BsHbDJyS5vBHK8mCkmF2tXvnpX+NdH79wscogCG2mv6M4Xa8YwAqXr3VLFM1dvTCOaRg7u4sS0IuW1w9t5WGpZ8FoED7Mb7N5J1GHYCYrwuXsiOof58v2cAYTlPPU=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjMD8/9BFjW0dKR2fAC4WX9peieC9RQrWAXUPa4YrcbwZGWXUEhcV3kjvmKvQ5d4Ix4fjcLm3IIti2dVakVzOfvtFpykUNA+las05LuF/HtkcuqvtWI/MiPpVucqh6y0BclyQUVT/YE9sP9hnCUKvH17MGXbaQtgfUyiNykm1ZxbS+h7JYhyd4/4clB3cY1Phjl3CVSv1M0KgSQxraTMvUhMfqVVzppiLfmojbOGJjJYjgowqI6ZL9biuPzqfkSWjFtl37J7/YkluuKd16/UxyK0NKbSQFg/pLmOdxWUxhW56gf1IO6E4KH2QF0ceC1l25WlXvqI2XPDqGbKJ6QJJBQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://hjl.mynatapp.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.shopmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
